package com.ball.dev.controller;


import com.ball.common.dtos.ResponseResult;
import com.ball.common.utils.UserContext;
import com.ball.dev.entity.po.Care;
import com.ball.dev.entity.po.Personal;
import com.ball.dev.entity.vo.CareVo;
import com.ball.dev.service.ICareService;
import com.ball.dev.service.IPersonalService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 关注
 * </p>
 *
 * @author lou
 * @since 2024-03-04
 */
@RestController
@RequestMapping("/care")
@RequiredArgsConstructor
public class CareController {

   private final  ICareService careService;
   private final IPersonalService personalService;
    @PostMapping("/addOrNot")
    public ResponseResult addCare(@RequestBody Care careDto){
        // 调用careService的addCare方法，传入careDto参数
        return careService.addCare(careDto);
    }

    @GetMapping("/{id}")
    public ResponseResult getCareByUserId(@PathVariable("id") Long id){
        // 调用careService的addCare方法，传入careDto参数
        return careService.getCareByUserId(id);
    }

    @GetMapping("/getFans")
    public ResponseResult getFans(){
        //获取当前用户id
        String userId = UserContext.getUser();
        //根据用户id和状态查询关注列表
        List<Care> cares = careService.lambdaQuery().eq(Care::getCareId, userId).eq(Care::getStatus, 0)
                .orderByDesc(Care::getCreateTime).list();
        //创建一个ArrayList用于存放查询结果
        ArrayList<CareVo> careVos = new ArrayList<>();
        //判断查询结果是否为空
        if (cares.size() > 0){
            //遍历查询结果
            for (Care care : cares) {
                //创建一个CareVo对象
                CareVo careVo = new CareVo();
                //将查询结果赋值给CareVo对象
                careVo.setCareId(care.getCareId());
                careVo.setUserId(care.getUserId());
                careVo.setId(care.getId());
                careVo.setCreateTime(care.getCreateTime());


                //根据用户id查询Personal对象
                Personal personal = personalService.lambdaQuery()
                        .eq(Personal::getUserId, care.getUserId())
                        .one();
                //将Personal对象中的信息赋值给CareVo对象
                careVo.setPhoto(personal.getPhoto());
                careVo.setUserName(personal.getUserName());
                //将CareVo对象添加到ArrayList中
                careVos.add(careVo);
            }
        }

        //返回查询结果
        return  ResponseResult.okResult(careVos);

    }

    @GetMapping("/getFollow")
    public ResponseResult getFollow() {
        //获取当前用户id
        String userId = UserContext.getUser();
        //根据用户id和状态查询关注列表
        List<Care> cares = careService.lambdaQuery().eq(Care::getUserId, userId).eq(Care::getStatus, 0)
                .orderByDesc(Care::getCreateTime).list();
        //创建一个ArrayList用于存放查询结果
        ArrayList<CareVo> careVos = new ArrayList<>();
        //判断查询结果是否为空
        if (cares.size() > 0){
            //遍历查询结果
            for (Care care : cares) {
                //创建一个CareVo对象
                CareVo careVo = new CareVo();
                //将查询结果赋值给CareVo对象
                careVo.setCareId(care.getCareId());
                careVo.setUserId(care.getUserId());
                careVo.setId(care.getId());
                careVo.setCreateTime(care.getCreateTime());


                //根据用户id查询Personal对象
                Personal personal = personalService.lambdaQuery()
                        .eq(Personal::getUserId, care.getCareId())
                        .one();
                if (personal != null){
                    //将Personal对象中的信息赋值给CareVo对象
                    careVo.setPhoto(personal.getPhoto());
                    careVo.setUserName(personal.getUserName());
                }

                //将CareVo对象添加到ArrayList中
                careVos.add(careVo);
            }
        }

        //返回查询结果
        return  ResponseResult.okResult(careVos);
    }





}
